﻿
using System.Collections.Generic;
using System.Linq;

using Irony.Compiler;

namespace IronLua.Ast {

	public class BlockStatement : Statement {

		readonly StatementList statements = new StatementList ();

		public StatementList Statements {
			get { return statements; }
		}

		public override LuaNodeType NodeType {
			get { return LuaNodeType.BlockStatement; }
		}

		public BlockStatement (CompilerContext context, NonTerminal nonTerminal, SourceLocation location, AstNodeList childNodes)
			: base (context, nonTerminal, location, childNodes)
		{
			statements.AddRange (CollectStatements ());
		}

		IEnumerable<Statement> CollectStatements ()
		{
			foreach (var node in ChildNodes [0].ChildNodes)
				yield return node.ChildNodes [0].ToStatement ();

			if (ChildNodes.Count > 1)
				yield return ChildNodes [1].ChildNodes [0].ToStatement ();
		}

		public override void Accept (ILuaAstVisitor visitor)
		{
			visitor.VisitBlockStatement (this);
		}
	}
}
